<?php

defined('ACC')||exit('Access Denied');

class CategoryModel extends Model{
	
	protected $rowCount=0;// 总行数
	protected $pageSize=0;// 每页显示大小
	protected $pageCount=0;// 总页数
	protected $pageNow=1;// 当前页
	protected $pageUp=0;// 上一页
	protected $pageDown=0;// 下一页
	protected $firstPage=0;// 首页
	protected $lastPage=0;// 尾页
	protected $maxPageSize=5;// 每页面显示几页
	protected $navigate="";// 翻页
	
	/* 设置分页信息 */
	public function setMessage($rowCount,$pageSize){
		$this->rowCount=$rowCount;
		$this->pageSize=$pageSize;
	
		$this->pageCount=ceil($rowCount/$pageSize);
	}
	
	/* 获取总行数 */
	public function getRowCount(){
		return $this->rowCount;
	}
	
	/* 获取每页显示大小 */
	public function getPageSize(){
		return $this->pageSize;
	}
	
	/* 获取总页数 */
	public function getPageCount(){
		return $this->pageCount;
	}
	
	/* 获取当前页 */
	public function getPageNow(){
		return $this->pageNow;
	}
	
	/* 修改当前页 */
	public function setPageNow($pageNow){
		$this->pageNow=$pageNow;
	}
	
	/* 修改每页面最大显示页数 */
	public function setMaxPageSize($maxPageSize){
		$this->maxPageSize=$maxPageSize;
	}
	
	/* 获取上一页 */
	public function getPageUp(){
		if($this->pageNow<=1){
			return $this->pageUp=1;
		}
		return $this->pageUp=pageNow-1;
	}
	
	/* 获取下一页 */
	public function getPageDown(){
		if($this->pageNow>=$this->pageCount){
			return $this->pageDown=$this->pageNow;
		}
		return $this->pageDown=$this->pageNow+1;
	}
	
	/* 获取没页面最大显示页数 */
	public function getMaxPageSize($maxPageSize){
		return $this->maxPageSize=$maxPageSize;
	}
	
	/* 获取总页数 */
	public function getPages(){
		$pages=array();
	
		$pages['rowCount']=$this->rowCount;
		$pages['pageSize']=$this->pageSize;
		$pages['pageCount']=$this->pageCount;
		$pages['nowCount']=$this->nowCount;
		$pages['pageUp']=$this->getPageUp();
		$pages['pageDown']=$this->getPageDown();
		$pages['firstPage']=$this->firstPage;
		$pages['lastPage']=$this->lastPage;
	
		return $pages;
	}
	
	/* 获取分页码 */
	public function getFenyeCode(){
		$pageNow=$this->pageNow;
 		$maxPageSize=$this->maxPageSize;
 		$pageNow2 = $pageNow;
 		
		if($pageNow%$maxPageSize==0){
			$end=$pageNow;
			$pageNow=$pageNow-$maxPageSize+1;
		}else{
			$pageNow=floor($pageNow/$maxPageSize)*$maxPageSize+1;
			$end=$pageNow+$maxPageSize-1;
		}
		
		$navigate="";
		$navigate.='<a href="javascript:;" >首页</a>&nbsp;';
		if($this->pageNow>=1){
			$prePage=$this->pageNow-1;
			$navigate.='<a href="javascript:;">上一页</a>&nbsp;';
		}
		
		if($pageNow2 > $maxPageSize){
			if(floor($this->pageCount/$maxPageSize) == floor(($pageNow2-1)/$maxPageSize)){
				$end = $this->pageCount;
			}
		}
		
		for($i=$pageNow;$i<=$end;$i++){
			$navigate.='<a href="javascript:;">';
			if($pageNow2 == $i){
				$navigate.='<font size="4" color="red">' . $i . '</font>';
			}else{
				$navigate.=$i;
			}
			$navigate.='</a>&nbsp;';
		}
		
		if($this->pageNow<=$this->pageCount){
			$nextPage=$this->pageNow+1;
			$navigate.='<a href="javascript:;">下一页</a>&nbsp;';
		}else{
			$nextPage=$this->pageCount;
			$navigate.='<a href="javascript:;">下一页</a>&nbsp;';
		}
		$navigate.='<a href="javascript:;">尾页</a>';
		
		return $navigate;
	
	}
	
	/* 获取一行栏目下的所有属性 */
	public function get_cat_attrs($cat_id){
		$sql = 'select attr_id, cat_id, attr_name, attr_input_type, attr_values from attribute where cat_id = ' . $cat_id;
		return $this->db->getAll($sql);
	}
	
	/* 获取所有商品 */
	public function get_goods($cat_id, $pageNow, $pageSize2){
		$sql = 'select goods_id, goods_name, shop_price, goods_thumb from goods where cat_id = ' . $cat_id . ' limit ' . (($pageNow-1)*$pageSize2) . ',' . $pageSize2;
		return $this->db->getAll($sql);
	}
	
	/* 获取栏目名 */
	public function get_cat_name($cat_id){
		$sql = 'select cat_name from category where cat_id = ' . $cat_id;
		return $this->db->getOne($sql);
	}
	
	/* 获取栏目父类信息 */
	public function get_parent($cat_id){
		$sql = 'select cat_id, cat_name from category where cat_id in (' . 'select parent_id from category where cat_id = ' . $cat_id . ')';
		return $this->db->getRow($sql);
	}
	
	/* 获取一行商品 */
	public function getRowCountGoods1($cat_id){
		$sql = 'select count(*) from goods where cat_id in ( select cat_id from category where parent_id = ' . $cat_id . ')';
		return $this->db->getOne($sql);
	}
	
	/* 获取一行商品 */
	public function getRowCountGoods2($cat_id){
		$sql = 'select count(*) from goods where cat_id = ' . $cat_id;
		return $this->db->getOne($sql);
	}
	
	/* 获取一行栏目下所有子栏目 */
	public function get_son_cat($cat_id){
		$sql = 'select cat_id from category where parent_id = $cat_id';
		return $this->db->getAll($sql);
		
	}
	
	/* 获取一行栏目下商品信息 */
	public function get_goods_info($cat_id, $pageNow, $pageSize){
		$sql = 'select goods_id, goods_name, market_price, shop_price, goods_thumb from goods where cat_id in ( select cat_id from category where parent_id = ' . $cat_id . ')' . ' limit ' . (($pageNow-1)*$pageSize) . ',' . $pageSize;
		return $this->db->getAll($sql);
	}
	
	/* 获取一行栏目下商品信息并排序 */
	public function get_goods_sort($cat_id, $sort, $pageNow, $pageSize){
		if($sort[0] != '' && $sort[1] != ''){
			$sql = 'select goods_id, goods_name, market_price, shop_price, goods_thumb from goods where cat_id in ( select cat_id from category where parent_id = ' . $cat_id . ') order by ' . $sort[0] . ' ' . $sort[1] . ' limit ' . (($pageNow-1)*$pageSize) . ',' . $pageSize;
		}else{
			$sql = 'select goods_id, goods_name, market_price, shop_price, goods_thumb from goods where cat_id in ( select cat_id from category where parent_id = ' . $cat_id . ') limit ' . (($pageNow-1)*$pageSize) . ',' . $pageSize;
		}
		return $this->db->getAll($sql);
	}
	
	/* 获取商品列表 */
	public function get_goods_list($cat_id, $attrs_value, $sort, $pageNow, $pageSize){
	
		$count = count($attrs_value);
		if($count == 0){
			if($sort[0] != '' && $sort[1] != ''){
				$sql = 'select goods_id, goods_name, shop_price, goods_brief, goods_thumb from goods where is_on_sale = 1 and cat_id = ' . $cat_id . ' order by ' . $sort[0] . ' ' . $sort[1] . ' limit ' . (($pageNow-1)*$pageSize) . ',' . $pageSize;
			}else{
				$sql = 'select goods_id, goods_name, shop_price, goods_brief, goods_thumb from goods where is_on_sale = 1 and cat_id = ' . $cat_id . ' limit ' . (($pageNow-1)*$pageSize) . ',' . $pageSize;
			}
			return $this->db->getAll($sql);
		}
		$sql = '';
		$sql .= 'select goods_id, goods_name, shop_price, goods_brief, goods_thumb from goods where is_on_sale = 1 and exists ( ';
		$sql .= ' select goods_id from ( ';
	
		if($count == 1){
			$attr_value = $attrs_value[0]['attr_value'];
	
			$sql .= ' select goods_id from ( select goods.goods_id, attr_value from goods left join goods_attr on goods.goods_id = goods_attr.goods_id where cat_id = ' . $cat_id . ') as temp where attr_value = ' . "'$attr_value'";
				
		}else if($count > 1){
			
			for($i=1; $i<$count; $i++){
				$sql .= ' select * from ( select temp.goods_id, goods_attr.attr_value from ( ';
			}
			$attr_value = $attrs_value[0]['attr_value'];
			$sql .= ' select * from ( ';
			$sql .= ' select goods.goods_id, attr_value from goods left join goods_attr on goods.goods_id = goods_attr.goods_id where cat_id = ' . $cat_id . ') as temp where attr_value = ' . "'$attr_value'";
			for($i=1; $i<$count; $i++){
				$attr_value = $attrs_value[$i]['attr_value'];
				$sql .= ' ) as temp left join goods_attr on temp.goods_id = goods_attr.goods_id	) as temp where attr_value = ' . "'$attr_value'";
			}
				
		}
	
		if($sort[0] != '' && $sort[1] != ''){
			$sql .= ' ) as temp where temp.goods_id = goods.goods_id and cat_id = ' . $cat_id . ') order by ' . $sort[0] . ' ' . $sort[1] . ' limit ' . (($pageNow-1)*$pageSize) . ',' . $pageSize;
		}else{
			$sql .= ' ) as temp where temp.goods_id = goods.goods_id and cat_id = ' . $cat_id . ')' . ' limit ' . (($pageNow-1)*$pageSize) . ',' . $pageSize;
		}
	
		return $this->db->getAll($sql);
	
	}
	
}



























/**
 select goods_id, goods_name, shop_price, goods_brief, goods_thumb from goods where is_on_sale = 1 and exists (
 select goods_id from (
 select * from (
 select temp.goods_id, goods_attr.attr_value from (
 select * from (
 select temp.goods_id, goods_attr.attr_value from (
 select * from (
 select goods.goods_id, attr_value from goods left join goods_attr on goods.goods_id = goods_attr.goods_id where cat_id = 11
 ) as temp where attr_value = '非触控'
 ) as temp left join goods_attr on temp.goods_id = goods_attr.goods_id
 ) as temp where attr_value = '华硕'
 ) as temp left join goods_attr on temp.goods_id = goods_attr.goods_id
 ) as temp where attr_value = '性能级显卡'
 ) as temp where temp.goods_id = goods.goods_id and cat_id = 11
 );
 */
